// +---------------------------------------------------------------------
// | 潮州市潮安区东凤镇浅沙软件开发工作室
// +---------------------------------------------------------------------
// | Copyright (c) 2016~2022 https://www.jacinto.cn All rights reserved.
// +---------------------------------------------------------------------
// | Licensed 浅沙建站系统并不是自由软件，未经许可不能去掉浅沙建站系统相关版权
// +---------------------------------------------------------------------
// | Author: 浅沙软件开发工作室 <qs_jacinto@qq.com>
// +---------------------------------------------------------------------
import { MutationTree } from 'vuex'
import { UserState } from './state'
import { UserMutationTypes } from './mutation-types'
import { setToken, setCache } from "@/utils/cache";
import Keys from '@/utils/constant/keys'
export type Mutations<S = UserState> = {
    [UserMutationTypes.SET_TOKEN](state: S, token: string): void
    [UserMutationTypes.SET_USER](state: S, user: any): void
    [UserMutationTypes.SET_ROLES](state: S, roles: Array<any>[]): void
    [UserMutationTypes.SET_PERMISSIONS](state: S, permissions: Array<string>[]): void
    [UserMutationTypes.SET_COMPANY](state: S, company: any): void
    [UserMutationTypes.SET_MENUS](state: S, menus: Array<any>[]): void
    [UserMutationTypes.SET_OSSCONFIG](state: S, token: any): void
}

export const mutations: MutationTree<UserState> & Mutations = {
    [UserMutationTypes.SET_TOKEN](state: UserState, token: string) {
        state.token = token;
        setToken(token)
    },

    [UserMutationTypes.SET_ROLES](state: UserState, roles: Array<any>[]) {
        state.roles = roles;
        setCache(Keys.roleKey, roles, 's')
    },

    [UserMutationTypes.SET_USER](state: UserState, user: any) {
        state.user = user;
        setCache(Keys.userInfoKey, user, 's')
    },

    [UserMutationTypes.SET_PERMISSIONS](state: UserState, permissions: Array<string>[]) {
        state.permissions = permissions;
        setCache(Keys.permissionKey, permissions, 's')
    },

    [UserMutationTypes.SET_COMPANY](state: UserState, company: any) {
        state.company = company;
        setCache(Keys.companyKey, company, 's')
    },
    [UserMutationTypes.SET_MENUS](state: UserState, menus: Array<any>[]) {
        state.menus = menus;
        setCache(Keys.menuKey, menus, 's')
    },
    [UserMutationTypes.SET_OSSCONFIG](state: UserState, config: any) {
        state.ossConfig = config;
    },
}
